package com.douyinkan.controller;

import com.douyinkan.entity.SystemConfig;
import com.douyinkan.service.SystemConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/config")
public class SystemConfigController {
    
    private static final Logger logger = LoggerFactory.getLogger(SystemConfigController.class);
    
    @Autowired
    private SystemConfigService systemConfigService;
    
    @GetMapping("/{key}")
    public ResponseEntity<SystemConfig> getConfig(@PathVariable String key) {
        logger.info("Getting config for key: {}", key);
        SystemConfig config = systemConfigService.getConfigByKey(key);
        if (config != null) {
            logger.info("Successfully retrieved config for key: {}, value: {}", key, config.getConfigValue());
            return ResponseEntity.ok(config);
        } else {
            logger.warn("Config not found for key: {}", key);
            return ResponseEntity.notFound().build();
        }
    }
    
    @PostMapping("/{key}")
    public ResponseEntity<SystemConfig> updateConfig(@PathVariable String key, @RequestBody ConfigUpdateRequest request) {
        logger.info("Updating config for key: {} with value: {}", key, request.getValue());
        SystemConfig config = systemConfigService.updateConfig(key, request.getValue());
        if (config != null) {
            logger.info("Successfully updated config for key: {}, new value: {}", key, config.getConfigValue());
            return ResponseEntity.ok(config);
        } else {
            logger.error("Failed to update config for key: {}", key);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    public static class ConfigUpdateRequest {
        private String value;
        
        public String getValue() {
            return value;
        }
        
        public void setValue(String value) {
            this.value = value;
        }
    }
}